#ifndef GASTOMENSUALMODULE_H
#define GASTOMENSUALMODULE_H

#include <QMainWindow>
#include <QMenu>
#include <QMenuBar>
#include <QToolBar>
#include <QStatusBar>
#include <QDialog>
#include <QSqlDatabase>
#include <QSqlRelationalTableModel>
#include <QTableView>
#include <QMessageBox>
#include <QAction>
#include <QDebug>
#include <QHeaderView>
#include <QGridLayout>
#include <QGroupBox>
#include <QFormLayout>
#include <QLineEdit>
#include <QDateEdit>
#include <QLabel>
#include <QSqlTableModel>
#include <QMessageBox>
#include <QSqlRecord>
#include <QSqlField>
#include <QSqlQuery>
#include <QSqlError>
#include <QRadioButton>
#include <QKeyEvent>
#include <QKeySequence>
#include <QShortcut>

#include "CustomToolbar.h"
#include "AltaGastoMensual.h"
#include "ModificacionGastoMensual.h"
#include "ABMConcepto.h"

class GastoMensualModule : public QMainWindow
{
    Q_OBJECT
public:
    explicit GastoMensualModule(QWidget *parent = 0);

signals:

public slots:
    void AltaGastoMesBtn();
    void BajaGastoMesBtn();
    void ModificaGastoMesBtn();
    void ActualizarListaGMBtn();
    void DividirGMBtn();
    void ABMConceptoBtn();
    void GastosAnteriores();
    // Filtros
    void FiltrarGastosLucas();
    void FiltrarGastosPablo();
    void FiltrarConcepto();
    void EncontrarConcepto();
    void FiltrarObservacion();
    void EncontrarObservacion();
    void FiltrarGastosFecha();

private:
    CustomModulesToolbar *gasto_mes_toolbar;

    QAction *action_alta_mes;
    QAction *action_baja_mes;
    QAction *action_modifica_mes;
    QAction *action_actualizar_mes;
    QAction *action_dividir_gastos;
    QAction *action_abmConcepto;

    QFormLayout *form_gastos;
    QFormLayout *form_deuda;

    QRadioButton *pago_lucas;
    QRadioButton *pago_pablo;
    QRadioButton *find_concepto;
    QRadioButton *find_observacion;

    QDateEdit *fecha_gasto;
    QDateEdit *fecha_gastos_anteriores;

    QLineEdit *find_line;

    void AltaGastoMensualToolbar();
    void BajaGastoMensualToolbar();
    void ModificacionGastoMensualToolbar();
    void ActualizarListaToolbar();
    void DividirGMToolbar();
    void ABMConceptoToolbar();

    void ActualizarValoresGastosMes();
    void ActualizarValoresGastosMesFiltro();
    void RefreshGastosMesView();
    void RefreshModelView();
};

class GMSqlTableModel : public QSqlRelationalTableModel
{
    Q_OBJECT
public:
    GMSqlTableModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase())
        : QSqlRelationalTableModel(parent,db) {;}
    QVariant data (const QModelIndex & index, int role = Qt::DisplayRole) const
    {
        if(role == Qt::BackgroundRole)
        {
            QModelIndex index2 = index.model()->index(index.row(), 7, index.parent()); // get index of "watched" column
            if (index.column() != 7) {
                if ((index2.data(Qt::DisplayRole).toString() == "Pablo") || (index2.data(Qt::DisplayRole).toString() == "pablo"))
                    return QVariant(QColor(Qt::darkGreen));
                else {
                    if ((index2.data(Qt::DisplayRole).toString() == "Lucas") || (index2.data(Qt::DisplayRole).toString() == "lucas"))
                        return QVariant(QColor(Qt::darkGray));
                    else {
                        if ((index2.data(Qt::DisplayRole).toString() == "Diego") || (index2.data(Qt::DisplayRole).toString() == "diego"))
                            return QVariant(QColor(Qt::darkCyan));
                    }
                }
            }
            else {
                const QVariant value(data(index,Qt::DisplayRole));
                if ((value.toString() == "Pablo") || (value.toString() == "pablo"))
                    return QVariant(QColor(Qt::darkGreen));
                else {
                    if ((value.toString() == "Lucas") || (value.toString() == "lucas"))
                        return QVariant(QColor(Qt::darkGray));
                    else {
                        if ((index2.data(Qt::DisplayRole).toString() == "Diego") || (index2.data(Qt::DisplayRole).toString() == "diego"))
                            return QVariant(QColor(Qt::darkCyan));
                    }
                }
            }
        }

        if(role == Qt::ForegroundRole)
        {
            QModelIndex index2 = index.model()->index(index.row(), 7, index.parent()); // get index of "watched" column
            if (index.column() != 7) {
                if ((index2.data(Qt::DisplayRole).toString() == "Pablo") || (index2.data(Qt::DisplayRole).toString() == "pablo"))
                    return QVariant(QColor(Qt::white));
                else {
                    if ((index2.data(Qt::DisplayRole).toString() == "Lucas") || (index2.data(Qt::DisplayRole).toString() == "lucas"))
                        return QVariant(QColor(Qt::white));
                    else {
                        if ((index2.data(Qt::DisplayRole).toString() == "Diego") || (index2.data(Qt::DisplayRole).toString() == "diego"))
                            return QVariant(QColor(Qt::white));
                    }
                }
            }
            else {
                const QVariant value(data(index,Qt::DisplayRole));
                if ((value.toString() == "Pablo") || (value.toString() == "pablo"))
                    return QVariant(QColor(Qt::white));
                else {
                    if ((value.toString() == "Lucas") || (value.toString() == "lucas"))
                        return QVariant(QColor(Qt::white));
                    else {
                        if ((value.toString() == "Diego") || (value.toString() == "diego"))
                            return QVariant(QColor(Qt::white));
                    }
                }
            }
        }

        if (role == Qt::TextAlignmentRole) {
            int col = index.column();
            if (col == 1 || col == 4 || col == 5) {
                return QVariant(Qt::AlignVCenter | Qt::AlignHCenter);
            }
            if (col == 3) {
                return QVariant(Qt::AlignVCenter | Qt::AlignRight);
            }
        }

        if (role == Qt::FontRole) {
            int col = index.column();
            if (col == 3)
                return QVariant (QFont("Helvetica57-Condensed",13,13));
            else
                return QVariant (QFont("Helvetica57-Condensed"));
        }

        return QSqlRelationalTableModel::data(index,role);
    }
};

#endif // GASTOMENSUALMODULE_H
